package banmuoi.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import banmuoi.entity.Customer;
import banmuoi.util.HibernateUtil;


@WebServlet("/crudCustomers.do")
public class CustomerAdmin extends HttpServlet {
	

    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Session session = HibernateUtil.openSession();
		
		if( request.getParameter("btnSearchID") != null )
		{
			String id ="";
			id = request.getParameter("txtTimId");

			String hql = "FROM Customer c where c.id =:id";
			Query query = session.createQuery(hql);
			query.setString("id", id);
			List<Customer> entities = query.list();
			request.setAttribute("showCustomers", entities);

		}
		else if( request.getParameter("btnSearchName") != null){
			String name = "";
			name = request.getParameter("txtTimName");
			String hql = "FROM Customer c where  c.name like :name";
			Query query = session.createQuery(hql);
			query.setString("name", "%"+name+"%");
			List<Customer> entities = query.list();
			request.setAttribute("showCustomers", entities);
		}
		else if(request.getParameter("btnSearchUser") != null){
			String user = "";
			user = request.getParameter("txtTimUser");
			String hql = "FROM Customer c where c.username =:user";
			Query query = session.createQuery(hql);

			query.setString("user", user);

			List<Customer> entities = query.list();
			request.setAttribute("showCustomers", entities);
		}
		else
		{
			String hql = "FROM Customer";
			Query query = session.createQuery(hql);
			List<Customer> entities = query.list();
			request.setAttribute("showCustomers", entities);
		}
		
				
		if(request.getParameter("customersId") != null){
			int customersId = Integer.parseInt(request.getParameter("customersId"));
			Customer entity = (Customer) session.load(Customer.class, customersId);
			request.setAttribute("entityCustomers", entity);
		}
					
		request.getRequestDispatcher("admin/CustomerAdmin.jsp").forward(request, response);
					
		session.close();
    }

    
    
    
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	String name, address, email, phone, username, password, photo, id;
		id = request.getParameter("txtId");
		name =request.getParameter("txtName");
		address =request.getParameter("txtAddress");
		email =request.getParameter("txtEmail");
		phone =request.getParameter("txtPhone");
		username =request.getParameter("txtUser");
		photo =request.getParameter("txtPhoto");
		
		Session session = HibernateUtil.openSession();
		Transaction trans = session.beginTransaction();
		
		try{
			if(request.getParameter("btnCreate") != null){
				Customer entity = new Customer();
				
				entity.setName(name);
				entity.setAddress(address);
				entity.setEmail(email);
				entity.setPhone(phone);
				entity.setUsername(username);
				entity.setPassword("123");
				entity.setPhoto(photo);

				
				session.save(entity);
				request.setAttribute("error", "Inserted successfully !");
			}
			else if(request.getParameter("btnUpdate") != null){
				Customer entity = new Customer();
				
				entity.setId(Integer.valueOf(id));
				entity.setName(name);
				entity.setAddress(address);
				entity.setEmail(email);
				entity.setPhone(phone);
				entity.setUsername(username);
				entity.setPhoto(photo);
				
				session.update(entity);
				request.setAttribute("error", "Updated successfully !");
			}
			else if(request.getParameter("btnDelete") != null){
				Customer entity = new Customer();
				
				entity.setId(Integer.valueOf(id));
				
				session.delete(entity);
				request.setAttribute("error", "Deleted successfully !");
			}
			
			
			trans.commit();
		}
		catch (Exception e) {
			trans.rollback();
			request.setAttribute("error", e.getMessage());
		}
		
		session.close();
		this.doGet(request, response);
	}

}
